package com.example.shoppingmall.cotroller;

import com.example.shoppingmall.config.MyLog;
import com.example.shoppingmall.pojo.Conditioned;
import com.example.shoppingmall.pojo.Consequence;
import com.example.shoppingmall.pojo.Sales;
import com.example.shoppingmall.service.CouponService;
import com.example.shoppingmall.service.SalesService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/sales")
public class SalesController {

    @Resource
    SalesService salesService;
    @Resource
    CouponService couponService;

    /**
     * 查询，分页，回显
     * @param dangIng
     * @param salesName
     * @param startType
     * @param exclusive
     * @param beginTime
     * @param endTime
     * @param model
     * @return
     */
    @RequestMapping("/salesList")
    public String salesList(
            @RequestParam(value = "dang", required = false) String dangIng,
            @RequestParam(value = "salesName", required = false) String salesName,
            @RequestParam(value = "startType", required = false) String startType,
            @RequestParam(value = "exclusive", required = false) String exclusive,
            @RequestParam(value = "beginTime", required = false) String beginTime,
            @RequestParam(value = "endTime", required = false) String endTime,
            Model model) {
        int dang = 1;
        if (dangIng != null & dangIng != "") {
            dang = Integer.parseInt(dangIng);
        }
        if (startType == null | startType == "") {
            startType = "0";
        }
        if (exclusive == null | exclusive == "") {
            exclusive = "0";
        }
        if (salesName == null) {
            salesName = "";
        }
        if (beginTime == null) {
            beginTime = "";
        }
        if (endTime == null) {
            endTime = "";
        }
        int size = 6;
        int count = salesService.count(salesName,Integer.parseInt(startType),Integer.parseInt(exclusive),beginTime,endTime);
        int zong = count % size == 0 ? count / size : count / size + 1;
        model.addAttribute("salesList", salesService.getAll(salesName,Integer.parseInt(startType),Integer.parseInt(exclusive),beginTime,endTime,dang, size));
        model.addAttribute("dang", dang);
        model.addAttribute("zong", zong);

        model.addAttribute("startType", startType);
        model.addAttribute("salesName", salesName);
        model.addAttribute("exclusive", exclusive);
        model.addAttribute("beginTime", beginTime);
        model.addAttribute("endTime", endTime);
        return "sales/sales";
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @MyLog(value = "促销管理->促销列表->删除")
    @RequestMapping("/del")
    @ResponseBody
    public Map<Object,Object> del(int id){
        Map<Object,Object> map=new HashMap<>();
        int num=salesService.del(id);
        if (num>0){
            map.put("del","yes");
        }else {
            map.put("del", "no");
        }
        return map;
    }

    /**
     * 跳到新增页面
     * @param model
     * @return
     */
    @MyLog(value = "促销管理->促销列表->跳到新增详情")
    @RequestMapping("/toAddSales")
    public String toAddSales(Model model){
        List<Conditioned> list = couponService.getAllConditioned();
        List<Consequence> list1 = couponService.getAllConsequence();
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);
        return "/sales/addSales";
    }

    /**
     * 新增
     * @param sales
     * @return
     */
    @MyLog(value = "促销管理->促销列表->添加")
    @RequestMapping("/addSales")
    public String addSales(Sales sales){
        int num=salesService.add(sales);
        if (num>0){
            return "redirect:/sales/salesList";
        }
        return "/sales/addSales";
    }

    /**
     * 跳到修改页面
     * @param id
     * @param model
     * @return
     */
    @MyLog(value = "促销管理->促销列表->跳到修改详情")
    @RequestMapping("/toUpdateSales")
    public String toUpdateCoupon(int id, Model model) {
        List<Conditioned> list = couponService.getAllConditioned();
        List<Consequence> list1 = couponService.getAllConsequence();
        model.addAttribute("list", list);
        model.addAttribute("list1", list1);
        model.addAttribute("salesById", salesService.getById(id));

        return "/sales/updateSales";
    }

    /**
     * 修改促销
     * @param sales
     * @return
     */
    @MyLog(value = "促销管理->促销列表->修改促销")
    @RequestMapping("/updateSales")
    public String updateCoupon(Sales sales) {
        int num = salesService.update(sales);
        if (num > 0) {
            return "redirect:/sales/salesList";
        }
        return "/sales/updateSales";
    }

}
